// 图书路由
const express = require("express");
// 引入connect-multiparty中间件
var multipart = require("connect-multiparty");
// 创建中间件对象
var multipartMiddleware = multipart();
// 引入数据库配置
const db = require("../mysql");
// 创建路由对象
const book = express.Router();

// 获取全部图书
book.get("/getAllBooks", multipartMiddleware, (req, res) => {
  const sqlStr = `select * from books where book_examine = '${req.query.book_examine}' and (book_name like '%${req.query.query}%' or book_author like '%${req.query.query}%' or book_seller like '%${req.query.query}%' or book_date like '%${req.query.query}%' or book_remarks like '%${req.query.query}%')`;
  db.query(sqlStr, (err, results) => {
    if (err) return res.json({ status: 404, message: error });
    res.json({ status: 200, data: results });
  });
})

// 根据书名查询图书
book.get("/getBooksByName", multipartMiddleware, (req, res) => {
  const sqlStr = `select * from books where book_name like '%${req.query.query}%'`
  db.query(sqlStr, (err, results) => {
    if (err) return res.json({ code: 400, message: error });
    res.json({ code: 200, data: results });
  });
})

// 根据分类查询图书
book.get("/getBooksByCate/:categoryId", multipartMiddleware, (req, res) => {
  const sqlStr = `select * from books where book_category_id = '${req.params.categoryId}'`
  db.query(sqlStr, (err, results) => {
    if (err) return res.json({ code: 400, message: error });
    res.json({ code: 200, data: results });
  });
})

// 获取已审核图书
book.get("/getBooks", multipartMiddleware, (req, res) => {
    const sqlStr = `select * from books where book_examine = '1' and book_num > '0'`;
    db.query(sqlStr, (err, results) => {
      if (err) return res.json({ status: 404, message: error });
      res.json({ status: 200, data: results });
    });
})
// 根据id获取图书
book.get("/getBookById/:bookId", multipartMiddleware, (req, res) => {
  const sqlStr = `select * from books where book_id = '${req.params.bookId}'`;
  db.query(sqlStr, (err, results) => {
    if (err) return res.json({ code: 404, message: error });
    res.json({ code: 200, data: results });
  });
})

// 根据发布者获取图书
book.get("/getBookBySeller/:seller", multipartMiddleware, (req, res) => {
  const sqlStr = `select * from books where book_seller = '${req.params.seller}'`;
  db.query(sqlStr, (err, results) => {
    if (err) return res.json({ code: 404, message: error });
    res.json({ code: 200, data: results });
  });
})

// 根据书名和发布者查询图书
book.get("/getBookBySellName", multipartMiddleware, (req, res) => {
  const sqlStr = `select * from books where book_name like '%${req.query.query}%' and book_seller='${req.query.username}'`
  db.query(sqlStr, (err, results) => {
    if (err) return res.json({ code: 400, message: error });
    res.json({ code: 200, data: results });
  });
})

// 根据id删除图书
book.post("/deleteBook", multipartMiddleware, (req, res) => {
  const sqlStr = `delete from books where book_id='${req.body.book_id}'`;
  db.query(sqlStr, (err, results) => {
    if (err) return res.json({ status: 404, message: "删除失败" });
    res.json({ status: 200, message: "删除成功" });
  });
});

// 设置书籍库存
book.put('/setBookNum', multipartMiddleware, (req, res) => {
  const sqlStr = `update books set book_num='${req.body.book_num}' where book_id='${req.body.book_id}'`
  db.query(sqlStr, (err, results) => {
      if (err) return res.json({ code: 400, message: '修改库存失败' });
      return res.json({ code: 200, message: '修改库存成功' })
  })
})

// 审核书籍
book.put('/examineBook', multipartMiddleware, (req, res) => {
  const sqlStr = `update books set book_examine='${req.body.book_examine}' where book_id='${req.body.book_id}'`
  db.query(sqlStr, (err, results) => {
      if (err) return res.json({ code: 400, message: '审核失败' });
      return res.json({ code: 200, message: '审核成功' })
  })
})

module.exports = book;
